/* ------------------------------------------------------------------ CLASS INTERFACE: matrix Tom Annau NOTE TO CNS 185 STUDENTS: This header file is written in C++. If you do not know C++, this file will be useless to you. Please refer to the file "example.cc" instead for instructions on how to use the "matrix" object. For those of you who do know C++, all methods are documented in this header file. There are many methods not demonstrated in "example.cc" which are in this header file, but they are not essential for completing the assignments. ------------------------------------------------------------------- */ #ifndef matrix_class #define matrix_class #include "vector.hh" class matrix { /* =================================================================== P U B L I C =================================================================== */ public: /* ----------------------------------------------------------------- Constructors and destructors ----------------------------------------------------------------- */ // Create a matrix, set all components to zero; rows and columns // must be > 0 or both = 0. matrix(int rows, int columns); // Create a (1 X N) matrix, where N is the dimension of the vector // and fill it with the vector's values matrix(const vector& v); // Copy constructor matrix(const matrix& m); // Create a matrix from a string; format is like "{3,2; 4,5}" matrix(const string& s); // Virtual destructor: deallocates free store space virtual ~matrix(void); /* ----------------------------------------------------------------- Assignment operators ----------------------------------------------------------------- */ // Copy one matrix's value to another, must be of same size matrix& operator = (const matrix& m); /* ----------------------------------------------------------------- Logical operators ----------------------------------------------------------------- */ // int operator == (const matrix & m) const; int operator != (const matrix & m) const; /* ----------------------------------------------------------------- Type conversions ----------------------------------------------------------------- */ string to_string(void) const; string table_form(void) const; /* ----------------------------------------------------------------- Accessing rows and columns ----------------------------------------------------------------- */ // The returned vector directly points to the data in the matrix: vector operator [] (int row_index) const; // The returned column is derived from the matrix, but modifying it // will have no impact on the matrix itself: vector column(int column_index) const; // Set the indexed column to be equal to the vector v. matrix & set_column(int column_index, const vector & v); // Return number of columns of matrix int columns(void) const; // Return number of rows of matrix int rows(void) const; /* ----------------------------------------------------------------- Arithmetic operations ----------------------------------------------------------------- */ // The following arithmetic operations are self-explanatory // In operators involving two matrices, they must be of the same // dimension, otherwise routine will complain matrix& operator += (const matrix& m); matrix operator + (const matrix& m) const; matrix operator - (void) const; matrix& operator -= (const matrix& m); matrix operator - (const matrix& m) const; matrix& operator += (double scalar); matrix operator + (double scalar) const; friend matrix operator + (double scalar, const matrix& m); matrix& operator -= (double scalar); matrix operator - (double scalar) const; friend matrix operator - (double scalar, const matrix& m); matrix& operator *= (double scalar); matrix operator * (double scalar) const; friend matrix operator * (double scalar, const matrix& m); matrix& operator /= (double scalar); matrix operator / (double scalar) const; matrix operator * (const matrix& m) const; vector operator * (const vector& v) const; // Matrix in this case must have only one row friend matrix operator * (const vector& v, const matrix& m); // Two vectors must be of same size friend matrix outer_product(const vector& v1, const vector& v2); /* ----------------------------------------------------------------- Miscellaneous ----------------------------------------------------------------- */ // Zero out all entries void zero(void); // Fill all entries with scalar void fill_with(double scalar); // Change matrix's dimension, zero out all entries void resize(int rows, int columns); // Return the identity matrix. Call as: A = matrix::identity(d); static matrix identity(int dim); // Return transpose of matrix friend matrix transpose(const matrix& m); // Return matrix which is transpose of vector (i.e., a row vector) friend matrix transpose(const vector& v); /* ----------------------------------------------------------------- Stream functions ----------------------------------------------------------------- */ // Output matrix to ostream in binary format friend ostream& operator < (ostream& out, const matrix& m); // Input matrix from istream in binary format friend istream& operator > (istream& in, matrix& m); // Output matrix to ostream in ASCII format friend ostream& operator << (ostream& out, const matrix& m); // Input matrix from istream in ASCII format friend istream& operator >> (istream& in, matrix& m); /* =================================================================== P R O T E C T E D =================================================================== */ protected: double **value; int n_rows, n_columns; int check_dims(const matrix& m) const; void copy_from(const matrix& m); void construct_from_string(string s); }; /* =================================================================== I n l i n e f u n c t i o n s =================================================================== */ /* ------------------------------------------------------------------- Constructors and destructors ------------------------------------------------------------------- */ inline matrix::matrix(const string& s) { construct_from_string(s); } /* ------------------------------------------------------------------- Accessing rows and columns ------------------------------------------------------------------- */ inline vector matrix::operator [] (int row_index) const return v(n_columns, value[row_index]) { if (row_index < 0 || row_index >= n_rows) cerr << "ERROR: Row index " << row_index << " is out of range for matrix\n"; } inline vector matrix::column(int c) const return v(n_rows); { if (c < 0 || c >= n_columns) cerr << "ERROR: Column index " << c << " is out of range for matrix\n"; else for (register int i = 0; i < n_rows; i++) v[i] = value[i][c]; } inline matrix & matrix::set_column(int c, const vector & v) { #ifdef RANGE_CHECKING if (c < 0 || c >= n_columns) cerr << "ERROR: In matrix::set_column(), index out of range\n"; else #endif if ( n_rows != v.dimension() ) cerr << "ERROR: In matrix::set_column(), vector must have same " "dimension as rows in the matrix!!\n"; else for( register int i=0; i